package com.itat.cases;

import com.itat.cases.innerClass.InnerClass;
import com.itat.cases.innerClass.InnerClasseExecute;

import java.util.*;

/**
 *  内部内局部变量测试
 * @author Darren bo
 * @version 1.0
 * @date 2021/5/14
 */
public class InnerClassCase {

    /**
     * 静态内部类，操作List
     * Q1-success: 正常：申明变量则不会出问题
     */
    public static void useInnerClass(){
        //list集合，内部类数据问题
        List<Integer> channelTypes=new ArrayList<>();
        channelTypes.add(1);
        if (channelTypes==null) {//set初始化值
            //赋值出现内部变量问题
            Set<Integer> channelTypesSet = new HashSet<>();
            channelTypesSet.add(2);
            channelTypes = new ArrayList<>(channelTypesSet);
            //
        }

        List<Integer> finalChannelTypes = channelTypes;//正文之前重新申明变量赋值ok
        InnerClasseExecute.execute(new InnerClass(){
            @Override
            public Object execute() {
                for (Integer channelType : finalChannelTypes) {//finalChannelTypes
                    System.out.println("channelType: "+channelType);
                }
                return null;
            }
        });

    }

    //Q1-err: 异常：报错不能执行
    //Variable 'channelTypes' is accessed from within inner class, needs to be final or effectively final
    /*public static void useInnerClass2(){
        List<Integer> channelTypes=new ArrayList<>();
        channelTypes.add(11);
        if (channelTypes==null) {//set初始化值
            //赋值出现内部变量问题
            Set<Integer> channelTypesSet = new HashSet<>();
            channelTypesSet.add(22);
            channelTypes = new ArrayList<>(channelTypesSet);
            //
        }
        InnerClasseExecute.execute(new InnerClass(){
            @Override
            public Object execute() {
                for (Integer channelType : channelTypes) {
                    System.out.println("channelType: "+channelType);
                }
                return null;
            }
        });
    }
*/

    //

    /**
     * 静态内部类，操作boolean变量
     * Q2-success 正常：申明则不会出问题
     */
    public static void useInnerClassForBoolean(){
        //Boolean
        final Boolean[] booFlag = {false};
        InnerClasseExecute.execute(new InnerClass(){
            @Override
            public Object execute() {
                booFlag[0] =true;
                return null;
            }
        });
        System.out.println("booFlag=: "+ booFlag[0]);

    }

    //Q2-err 异常：报错不能执行
    /*public static void useInnerClassForBoolean2(){
        //Boolean
        Boolean booFlag=true;
        InnerClasseExecute.execute(new InnerClass(){
            @Override
            public Object execute() {
                booFlag =false;
                return null;
            }
        });
        System.out.println("booFlag=: "+ booFlag);
    }*/

}
